﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace RuleCode.Common
{
    #region AOTQueryProperty  类
    public struct AOTQueryProperty
    {
        private string name;
        /// <summary>
        /// 获取或设置Query 的名称
        /// </summary>
        public string Name
        {
            get { return this.name;  }
            set { this.name = value; }
        }

        private string formName;
        /// <summary>
        /// 获取或设置 Query 相对应的窗体的名称
        /// </summary>
        public string FormName
        {
            get { return this.formName;  }
            set { this.formName = value; }
        }

        private System.Type formType;
        /// <summary>
        /// 获取或设置 Query 相对应的窗体类型
        /// </summary>
        public System.Type FormType
        {
            get { return this.formType;  }
            set { this.formType = value; }
        }

        private bool userUpdate;
        /// <summary>
        /// 
        /// </summary>
        public bool UserUpdate
        {
            get { return this.userUpdate;  }
            set { this.userUpdate = value; }
        }

        private bool allowCheck;
        /// <summary>
        /// 
        /// </summary>
        public bool AllowCheck
        {
            get { return this.allowCheck;  }
            set { this.allowCheck = value; }
        }

        private bool interActive;
        /// <summary>
        /// 
        /// </summary>
        public bool InterActive
        {
            get { return this.interActive;  }
            set { this.interActive = value; }
        }
    }
    #endregion

    #region AOTQueryDataSourceProperty 类
    public struct  AOTQueryDataSourceProperty
    {
        private string name;
        /// <summary>
        /// 获取或设置 DataSource 的名称
        /// </summary>
        public string Name
        {
            get { return this.name;  }
            set { this.name = value; }
        }

        private string tableName;
        /// <summary>
        /// 获取或设置DataSource相对应的表名
        /// </summary>
        public string TableName
        {
            get { return this.tableName;  }
            set { this.tableName = value; }
        }

        private System.Type tableType;
        /// <summary>
        /// 获取或设置 DataSource相对应的表的类型
        /// </summary>
        [Browsable (false )]
        public System.Type TableType
        {
            get { return this.tableType;  }
            set { this.tableType = value; }
        }

        private bool firstOnly;
        /// <summary>
        /// Only the first record?
        /// </summary>
        public bool FirstOnly
        {
            get { return this.firstOnly;  }
            set { this.firstOnly = value; }
        }

        private bool firstFast;
        /// <summary>
        /// Retrieve the first record faster than next?
        /// </summary>
        public bool FirstFast
        {
            get { return this.firstFast;  }
            set { this.firstFast = value; }
        }

        private FetchMode fetchMode;
        /// <summary>
        /// 获取或设置 主从表获取模式
        /// </summary>
        public FetchMode FetchMode
        {
            get { return this.fetchMode;  }
            set { this.fetchMode = value; }
        }

        private OrderMode orderMode;
        /// <summary>
        /// 获取或设置 排序模式
        /// </summary>
        public OrderMode OrderMode
        {
            get { return this.orderMode;  }
            set { this.orderMode = value; }
        }

        private JoinMode joinMode;
        /// <summary>
        /// 获取或设置 主从表的 Join 的模式
        /// </summary>
        public JoinMode JoinMode
        {
            get { return this.joinMode;  }
            set { this.joinMode = value; }
        }

        private AllowAdd allowAdd;
        /// <summary>
        /// 获取或设置 增加的方式 all fields 或者 no fields
        /// </summary>
        public AllowAdd AllowAdd
        {
            get { return this.allowAdd;  }
            set { this.allowAdd = value; }
        }

        private bool update;
        /// <summary>
        /// Specify whether the cursor can update records in database
        /// </summary>
        public bool Update
        {
            get { return this.update;  }
            set { this.update = value; }
        }

        private bool relations;
        /// <summary>
        /// specify whether the database relations used for data source linking are explicitly given
        /// </summary>
        public bool Relations
        {
            get { return this.relations;  }
            set { this.relations = value; }
        }

        private bool enabled;
        /// <summary>
        /// Specifies whether the data source  (and all embeded data source ) should be ignored
        /// </summary>
        public bool Enabled
        {
            get { return this.enabled;  }
            set { this.enabled = value; }
        }

        private bool selectWithRepeatalbleRead;
        /// <summary>
        /// Specifies whether the data source should use repeatable read hint when doing select
        /// </summary>
        public bool SelectWithRepeatalbleRead
        {
            get { return this.selectWithRepeatalbleRead;  }
            set { this.selectWithRepeatalbleRead = value; }
        }

        private OncurrencyMode oncurrencyMode;
        /// <summary>
        /// Specifies which oncurrency mode (auto ,pessmistic or optimistic) the data source should use when doing select
        /// </summary>
        public OncurrencyMode OnCurrencyMode
        {
            get { return this.oncurrencyMode;  }
            set { this.oncurrencyMode = value; }
        }


        public object this[string caption]
        {
            get
            {
                switch (caption)
                {
                    case "Name":
                        {
                            return this.Name ;
                        }
                    case "TableName":
                        {
                            return this.TableName ;
                        }
                    case "TableType":
                        {
                            return this.TableType ;
                        }
                    case "AllowAdd":
                        {
                            return this.AllowAdd ;
                        }
                    case "Enabled":
                        {
                            return this.Enabled ;
                        }
                    case "FetchMode":
                        {
                            return this.FetchMode ;
                        }
                    case "FirstFast":
                        {
                            return this.FirstFast ;
                        }
                    case "FirstOnly":
                        {
                            return this.FirstOnly ;
                        }
                    case "JoinMode":
                        {
                            return this.JoinMode ;
                        }
                    case "OnCurrencyMode":
                        {
                            return this.OnCurrencyMode ;
                        }
                    case "Relations":
                        {
                            return this.Relations ;
                        }
                    case "SelectWithRepeatalbleRead":
                        {
                            return this.SelectWithRepeatalbleRead ;
                        }
                    case "Update":
                        {
                            return this.Update ;
                        }
                    case "OrderMode":
                        {
                            return this.OrderMode ;
                        }
                    default:
                        {
                            return null;
                        }
                }
            }
            set
            {
                switch (caption)
                {
                    case "Name":
                        {
                            this.Name = value.ToString();
                            break; 
                        }
                    case "TableName":
                        {
                            this.TableName = value.ToString() ;
                            break;
                        }
                    case "TableType":
                        {
                            this.TableType= value as System.Type ;
                            break;
                        }
                    case "AllowAdd":
                        {
                            this.AllowAdd = (AllowAdd )value ;
                            break;
                        }
                    case "Enabled":
                        {
                            this.Enabled = (bool) value ;
                            break;
                        }
                    case "FetchMode":
                        {
                            this.FetchMode= (FetchMode )value ;
                            break;
                        }
                    case "FirstFast":
                        {
                            this.FirstFast = (bool)value ;
                            break;
                        }
                    case "FirstOnly":
                        {
                            this.FirstOnly =(bool)value ;
                            break;
                        }
                    case "JoinMode":
                        {
                            this.JoinMode = (JoinMode )value ;
                            break;
                        }
                    case "OnCurrencyMode":
                        {
                            this.OnCurrencyMode = (OncurrencyMode)value ;
                            break;
                        }
                    case "Relations":
                        {
                            this.Relations = (bool )value ;
                            break;
                        }
                    case "SelectWithRepeatalbleRead":
                        {
                            this.SelectWithRepeatalbleRead =(bool)value ;
                            break;
                        }
                    case "Update":
                        {
                            this.Update =(bool)value ;
                            break;
                        }
                    case "OrderMode":
                        {
                            this.OrderMode = (OrderMode )value ;
                            break;
                        }
                    default:
                        {
                            return;
                        }
                }
            }
        }
    }
    #endregion

    public class FieldCollection : RuleCode.ObjectTree.AdvBaseStringCollection
    {
        public static List<string> MyList = new List<string>();  

        protected override void SetStandardValuesCollection()
        {
            if (value == null)
            {
                value = new StandardValuesCollection(MyList );
            }
            base.SetStandardValuesCollection();
        }
    }

    #region AOTQueryDataSourceRelation
    /// <summary>
    /// 获取QueryDataSource之间的Relation
    /// </summary>
    public struct AOTQueryDataSourceRelation
    {
        private string field;
        /// <summary>
        /// 获取或设置 从表字段名称
        /// </summary>
        [TypeConverter (typeof (FieldCollection ))]
        public string Field
        {
            get { return this.field; }
            set { this.field = value; }
        }

        private string relationField;
        /// <summary>
        /// 获取或设置 主表相对应的字段名称
        /// </summary>
        [TypeConverter (typeof (FieldCollection ))]
        public string RelationField
        {
            get { return this.relationField; }
            set { this.relationField = value; }
        }

        private string table; 
        /// <summary>
        /// 获取或设置 从表的表名
        /// </summary>
        public string Table
        {
            get { return this.table; }
            set { this.table = value; }
        }

        private string relationTable;
        /// <summary>
        /// 获取或设置 主表表名
        /// </summary>
        public string RelationTable
        {
            get { return this.relationTable; }
            set { this.relationTable = value; }
        }

        public object this[string caption]
        {
            get 
            {
                switch (caption)
                {
                    case "Table":
                        {
                            return this.Table ;
                        }
                    case "Field":
                        {
                            return this.Field ;
                        }
                    case "RelationTable":
                        {
                            return this.RelationTable ;
                        }
                    case "RelationField":
                        {
                            return this.RelationField ;
                        }
                    default :
                        return null;
                }
            }
            set
            {
                switch (caption)
                {
                    case "Table":
                        {
                            this.Table = value.ToString();
                            break;
                        }
                    case "Field":
                        {
                            this.Field = value.ToString();
                            break;
                        }
                    case "RelationTable":
                        {
                            this.RelationTable = value.ToString();
                            break;
                        }
                    case "RelationField":
                        {
                            this.RelationField = value.ToString();
                            break;
                        }
                    default:
                        return;
                }
            }
        }
    }
    #endregion

    #region AOTQuerySorting
    public struct AOTQuerySorting
    {
        private string fieldName;
        /// <summary>
        /// 获取或设置 排序的字段名
        /// </summary>
        public string FieldName
        {
            get { return this.fieldName;  }
            set { this.fieldName = value; }
        }

        private string tableName;
        /// <summary>
        /// 获取或设置 排序的表名
        /// </summary>
        public string TableName
        {
            get { return this.tableName;  }
            set { this.tableName = value; }
        }

        private SortingType ordering;
        /// <summary>
        /// 获取或设置 排序的方向 SortingType.ASC or Sorting.DESC
        /// </summary>
        public SortingType Ordering
        {
            get { return this.ordering;  }
            set { this.ordering = value; }
        }
    }
    #endregion

    #region AOTQueryRange
    public struct AOTQueryRange
    {
        private string fieldName;
        /// <summary>
        /// 获取或设置 过滤条件的字段名
        /// </summary>
        public string FieldName 
        {
            get { return this.fieldName;  }
            set { this.fieldName = value; }
        }

        private System.Type fieldType;
        /// <summary>
        /// 获取或设置 过滤条件对应的表字段的类型
        /// </summary>
        public System.Type FieldType
        {
            get { return this.fieldType;  }
            set { this.fieldType = value; }
        }

        private string tableName;
        /// <summary>
        /// 获取或设置 过滤条件的表名
        /// </summary>
        public string TableName
        {
            get { return this.tableName;  }
            set { this.tableName = value; }
        }

        private System.Type tableType;
        /// <summary>
        /// 获取或设置 过滤条件对应的表的类型
        /// </summary>
        public System.Type TableType
        {
            get { return this.tableType;  }
            set { this.tableType = value; }
        }

        private object value;
        /// <summary>
        /// 获取或设置 过滤条件的值
        /// </summary>
        public object Value
        {
            get { return this.value;  }
            set { this.value = value; }
        }

        private string label;
        /// <summary>
        /// 获取或设置 Label值
        /// </summary>
        public string Label
        {
            get { return this.label;  }
            set { this.label = value; }
        }

        private RangeStatus status;
        /// <summary>
        /// 获取或设置 过滤条件的状态 RangeStatus.Open RangeStatus.Hide,RangeStatus.Lock
        /// </summary> 
        public RangeStatus Status
        {
            get { return this.status;  }
            set { this.status = value; }
        }

        private bool enabled;
        /// <summary>
        /// 获取或设置 是否启用过滤条件
        /// </summary>
        public bool Enabled
        {
            get { return this.enabled;  }
            set { this.enabled = value; }
        }
    }
    #endregion

    #region enum FetchMode
    public enum FetchMode
    {
        /// <summary>
        /// 1:n
        /// </summary>
        OneToMore=0,

        /// <summary>
        /// 1:1
        /// </summary>
        OneToOne

    }
    #endregion

    #region enum OrderMode
    public enum OrderMode
    {
        /// <summary>
        /// Order By
        /// </summary>
        OrderBy=0,

        /// <summary>
        /// Group By
        /// </summary>
        GroupBy
    }
    #endregion

    #region enum JoinMode
    public enum JoinMode
    {
        /// <summary>
        /// 主从表的Join的模式为(left) outer join
        /// </summary>
        OuterJoin=0,

        /// <summary>
        /// 主从表的Join的模式为 inner join
        /// </summary>
        InnerJoin,        

        /// <summary>
        /// 主从表的Join的模式为 distinct inner join
        /// </summary>
        ExistsJoin,

        /// <summary>
        /// 主从表的Join的模式为 ExistsJoin 的取反
        /// </summary>
        NoExistsJoin
    }
    #endregion

    #region enum AllowAdd
    public enum AllowAdd
    {
        /// <summary>
        /// 所有的字段允许增加
        /// </summary>
        AllFields=0,

        /// <summary>
        /// 不允许增加
        /// </summary>
        NoField
    }
    #endregion

    #region enum OncurrencyModel
    public enum OncurrencyMode
    {
        /// <summary>
        /// 
        /// </summary>
        Auto=0,

        /// <summary>
        /// 
        /// </summary>
        Optimistic,

        /// <summary>
        /// 
        /// </summary>
        pessimistic
    }
    #endregion

    #region enum RangeStatus
    public enum RangeStatus
    { 
        /// <summary>
        /// 
        /// </summary>
        Open=0,

        /// <summary>
        /// 
        /// </summary>
        Lock,

        /// <summary>
        /// 
        /// </summary>
        Hide
    }
    #endregion
}
